package cemp.rmq;

import cemp.redis.util.RedisUtils;
import cemp.service.FetchDataService;
import cemp.service.StockKpiDayService;
import cemp.thread.StockAvgTask;
import cemp.thread.TestTask;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

@Component
@Slf4j
public class StockRMQAvgListener {

    @Autowired
    FetchDataService fetchDataService;
    @Autowired
    StockKpiDayService stockKpiDayService;
    @Autowired
    RedisUtils redisUtils;
    @Autowired
    ThreadPoolTaskExecutor stockAvgExecutor;



    @RabbitListener(queues = "stockAvgQueue")
    public void ListerAck(Message message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException, ClassNotFoundException, InterruptedException {
        ByteArrayInputStream strem =new ByteArrayInputStream(message.getBody());
        ObjectInputStream strem2 =new ObjectInputStream(strem);
        Map data = (Map)strem2.readObject();
        String  stockCode = (String)data.get("stockCode");
        String startDate = (String)data.get("startDate");
        String endDate = (String)data.get("endDate");
        //fetchDataService.addAvg2(startDate,endDate,stockCode);
        //使用线程池处理任务
        stockAvgExecutor.submit(new StockAvgTask(fetchDataService,startDate,endDate,stockCode));
    }

}
